﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Controls;
using System.Collections;

namespace TeamReview.UI
{
    /// <summary>
    /// This form contains alot of code From Brian Randell's MSDN Magazine Simple Work Item Explorer Article
    /// </summary>
    /// <see cref="http://msdn2.microsoft.com/en-us/magazine/cc163443.aspx"/>
    public partial class WorkItemWindow : Form
    {
        private WorkItemFormControl workItemControl;
        public WorkItemWindow() : this(null) { }

        public WorkItemWindow(WorkItem workItem)
        {
            InitializeComponent(); 
            if (workItem == null) 
                return;

            SetupWorkItemControl(workItem);
            
        }

        private void SetupWorkItemControl(WorkItem workItem)
        {


            if (workItemControl == null)
            {
                workItemControl = new WorkItemFormControl();
                workItemControl.Dock = DockStyle.Fill;
                workItemControl.BackColor = System.Drawing.SystemColors.Window;
                workItemControl.ReadOnly = false;
                panel1.Controls.Add(workItemControl);
            }
            workItem.Open();
            workItem.FieldChanged += new WorkItemFieldChangeEventHandler(workItem_FieldChanged);
            workItemControl.Item = workItem;
            SetTitle();
        }

        void workItem_FieldChanged(object sender, Microsoft.TeamFoundation.WorkItemTracking.Client.WorkItemEventArgs e)
        {
            SetTitle();
        }

        private void SetTitle()
        {
            string format = "{0} {1}";
            if (workItemControl != null && workItemControl.Item != null)
            {
                if(workItemControl.Item.IsDirty)
                    format += "*";
                Text = String.Format(format, CONSTANTS.CODE_REVIEW_ITEM_TYPE, workItemControl.Item.Id);
            }
            else
            {
                Text = CONSTANTS.CODE_REVIEW_ITEM_TYPE;
            }
        }

        private void WorkItemWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (workItemControl.Item.IsDirty)
            {
                switch (MessageBox.Show("Would you like to save your changes?", "Save Changes", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question))
                {
                    case System.Windows.Forms.DialogResult.Yes:
                        e.Cancel = !Save();
                        break;
                    case System.Windows.Forms.DialogResult.No:
                        workItemControl.Item.Reset();
                        break;
                    case System.Windows.Forms.DialogResult.Cancel:
                        e.Cancel = true;
                        break;
                }
            }
        }

        private bool Save()
        {
            if (!workItemControl.Item.IsDirty)
                return true;

            ArrayList badFields = workItemControl.Item.Validate();
            if (workItemControl.Item.IsValid())
            {
                workItemControl.Item.Save();
                SetTitle();
                return true;
            }
           
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("The following fields are invalid: ");
            foreach (Field field in badFields)
            {
                if (!field.IsValid)
                    builder.AppendLine(field.Name);
            }

            MessageBox.Show(builder.ToString(), "Invalid fields", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
            Close();
        }
    }
}
